home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 08 Zarozinski / src / ffllapi / MOMDefuzzVarObj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-13  |  4.3 KB  |  190 lines

  1. //
  2. // File:    MOMDefuzzVarObj.cpp    
  3. //
  4. // Purpose:    Variable object for Mean of Maximum Defuzzification method
  5. //
  6. // Copyright ⌐ 2001 Louder Than A Bomb! Software
  7. //
  8. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  9. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  10. //
  11. // 
  12. #include "MOMDefuzzVarObj.h"
  13. #include "MOMDefuzzSetObj.h"
  14. #include "FuzzyOutSet.h"
  15. #include "FuzzyOutVariable.h"
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. //
  23. // Function:    MOMDefuzzVarObj()
  24. // 
  25. // Purpose:        Constructor
  26. //
  27. // Arguments:
  28. //
  29. //        FuzzyOutVariable* par - variable that this is part of
  30. //
  31. // Returns:
  32. //
  33. //        none
  34. //
  35. // Author:    Michael Zarozinski
  36. // Date:    6/01
  37. // 
  38. // Modification History
  39. // Author    Date        Modification
  40. // ------    ----        ------------
  41. //
  42. //
  43.  
  44. MOMDefuzzVarObj::MOMDefuzzVarObj(FuzzyOutVariable* _parent): DefuzzVarObj(_parent), FFLLBase(_parent)
  45. {
  46.     // nothing to do
  47. }
  48. //
  49. // Function:    ~MOMDefuzzVarObj()
  50. // 
  51. // Purpose:        Destructor
  52. //
  53. // Arguments:
  54. //
  55. //        none
  56. //
  57. // Returns:
  58. //
  59. //        none
  60. //
  61. // Author:    Michael Zarozinski
  62. // Date:    6/01
  63. // 
  64. // Modification History
  65. // Author    Date        Modification
  66. // ------    ----        ------------
  67. //
  68. //
  69.  
  70. MOMDefuzzVarObj::~MOMDefuzzVarObj()
  71. {
  72.     // nothing to do
  73. }
  74.  
  75.  
  76. //
  77. // Function:    calc_value()
  78. // 
  79. // Purpose:        Calculate the defuzzified output value (using the MOM 
  80. //                defuzzification method) for the output variable.
  81. //
  82. // Arguments:    
  83. //
  84. //        DOMType* out_set_dom_arr -    Array that holds the DOM value for each
  85. //                                    set in the output variable
  86. //
  87. // Returns:
  88. //
  89. //        RealType - the defuzzified output value. FLT_MIN is returned if no output sets are active
  90. //
  91. // Author:    Michael Zarozinski
  92. // Date:    8/01
  93. // 
  94. // Modification History
  95. // Author    Date        Modification
  96. // ------    ----        ------------
  97. //
  98. //
  99. RealType MOMDefuzzVarObj::calc_value(DOMType* out_set_dom_arr  )
  100. {
  101.     FuzzyOutVariable*    parent;        // pointer to parent
  102.     int                    num_of_sets;// number of sets
  103.      int                    i;            // counter
  104.      MOMDefuzzSetObj*    defuzz;        // defuzzification object for the set
  105.     MOMDefuzzSetObj*    winning_defuzz = NULL;        //defuzzification object for the WINNING set
  106.     FuzzyOutSet*        set;        // set to deal with
  107.      DOMType                mom_max;    // max MOM from sets
  108.     DOMType                mom_idx;    // MOM index for the DOM
  109.     int                    set_idx = -1;// winning set idx
  110.  
  111.     parent  = get_parent();
  112.     assert(parent);
  113.  
  114.     num_of_sets = parent->get_num_of_sets();
  115.  
  116.     mom_max = 0;
  117.  
  118.     // find the highest DOM for the output sets
  119.     for (i = 0; i < num_of_sets; ++i)
  120.         {
  121.         set = parent->get_set(i);
  122.  
  123.         defuzz = dynamic_cast<MOMDefuzzSetObj*>(set->get_defuzz_obj());  
  124.  
  125.           mom_idx = out_set_dom_arr[i];  
  126.         if (mom_idx == 255)
  127.             mom_idx = 0;
  128.  
  129.         // if mom_idx is greater, save it
  130.         if (mom_max < mom_idx)
  131.             {
  132.             mom_max = mom_idx;
  133.             winning_defuzz = defuzz;
  134.             }
  135.  
  136.         } // end loop through sets
  137.  
  138.     if (!winning_defuzz)
  139.         {
  140.         // no output set value to FLT_MIN - the special value that
  141.         // ensures we know that there is no output
  142.          return FLT_MIN;    // don't div by 0... just return
  143.         }
  144.  
  145.     return (winning_defuzz->get_mean_value( ));
  146.  
  147. } // end MOMDefuzzVarObj::calc_value()
  148.  
  149.  
  150. //
  151. // Function:    get_set_defuzz_obj()
  152. // 
  153. // Purpose:        Return the defuzzification object of the correct type
  154. //
  155. // Arguments:
  156. //
  157. //        int set_idx - index of the set to get the defuzzifiation object for
  158. //
  159. // Returns:
  160. //
  161. //        MOMDefuzzSetObj* - defuzzification object for the set
  162. //
  163. // Author:    Michael Zarozinski
  164. // Date:    8/01
  165. // 
  166. // Modification History
  167. // Author    Date        Modification
  168. // ------    ----        ------------
  169. //
  170. //
  171. MOMDefuzzSetObj* MOMDefuzzVarObj::get_set_defuzz_obj(int set_idx) const
  172. {
  173.     FuzzyOutVariable* parent = get_parent();
  174.     FuzzyOutSet* set = parent->get_set(set_idx);
  175.  
  176.     MOMDefuzzSetObj* tmp_obj = dynamic_cast<MOMDefuzzSetObj*>(set->get_defuzz_obj()); 
  177.     
  178.     return tmp_obj;
  179.  
  180. } // end MOMDefuzzVarObj::get_set_defuzz_obj()
  181.  
  182. /////////////////////////////////////////////////////////////////////
  183. ////////// Trivial Functions That Don't Require Headers /////////////
  184. /////////////////////////////////////////////////////////////////////
  185.  
  186. int MOMDefuzzVarObj::get_defuzz_type() const 
  187.     return DefuzzVarObj::DEFUZZ_TYPE::MOM; 
  188. };
  189.